<?php

/**
 * Brand form.
 *
 * @package    virtualstore
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class BrandForm extends BaseBrandForm {

    public function configure() {
        unset($this->widgetSchema['logo']);
        unset($this->validatorSchema['logo']);

//        $this->widgetSchema['logo'] = new sfWidgetFormInputFileEditable(array(
//                    'label' => 'Logo',
//                    'file_src' => '/uploads/' . $this->getObject()->getLogo(),
//                    'is_image' => true,
//                    'edit_mode' => !$this->isNew(),
//                    'template' => '<div>%file%<br />%input%<br />%delete% %delete_label%</div>',
//                ));
//        $this->validatorSchema['logo_delete'] = new sfValidatorBoolean();
        $this->setWidget('file', new sfWidgetFormInputFile());
        $this->setValidator('file', new sfValidatorFile(array(
                    'required' => false,
                    'path' => sfConfig::get('sf_web_dir') . '/images/brands',
                    'mime_types' => 'web_images',
                )));
//        $this->setValidator('logo', new sfValidatorFile(array(
//                    'required' => false,
//                    'path' => sfConfig::get('sf_upload_dir'),
//                    'mime_types' => 'web_images',
//                )));

        //unset($this->widgetSchema['created_at']);
        $this->setWidget('created_at',
                new sfWidgetFormPlain(
                array('value' =>$this->getObject()->getCreatedAt())));
        unset($this->validatorSchema['created_at']);

        //unset($this->widgetSchema['updated_at']);
        $this->setWidget('updated_at',
                new sfWidgetFormPlain(
                array('value' =>$this->getObject()->getUpdatedAt())));
        unset($this->validatorSchema['updated_at']);

        #Setting updated_at read only
        $this->setWidget('updated_by',
                new sfWidgetFormPlain(
                array('value' =>$this->getObject()->getUpdatedBy())));
        // $this->getObject()->getUpdatedBy()
        //unset($this->widgetSchema['updated_by']);
       unset($this->validatorSchema['updated_by']);

       $this->setWidget('created_by',
                new sfWidgetFormPlain(
                array('value' =>$this->getObject()->getUpdatedBy())));
        // $this->getObject()->getUpdatedBy()
        //unset($this->widgetSchema['updated_by']);
       unset($this->validatorSchema['created_by']);
    }

    protected function updateFileColumn($value) {
        return false;
    }

    protected function doSave($con = null) {
        $log = sfContext::getInstance( )->getLogger();
        $log->debug("****** doSave ******");
        $file = $this->values['file'];
        if ($file instanceof sfValidatedFile) {
            $filename = sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . 'images'
                    . DIRECTORY_SEPARATOR . 'brands'
                    . DIRECTORY_SEPARATOR . $file->getOriginalName();

            // $filename = sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR .$file->getOriginalName();
            $log->debug("***** Output File: " . $filename);
            $this->values['logo'] = "brands/" . $file->getOriginalName();
            $file->save($filename, 0666, true, 0777);
        }
        parent::doSave($con);
    }

}
